package com.wzcl.app.controller;

import com.wzcl.app.dao.model.Course;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.CourseDto;
import com.wzcl.app.model.vo.CourseVo;
import com.wzcl.app.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Log4j2
@RestController
@RequestMapping("/course")
@Api(value="课程管理controller",tags={"课程管理接口"})
public class CourseController {
    @Resource
    private CourseService courseService;

    @ApiOperation("添加课程")
    @PostMapping("/add")
    public RVO<CourseVo> addCourse(@RequestBody @Valid CourseDto courseDto){
        Course course = courseService.addCourse(courseDto);
        return RVO.success(CourseVo.create(course));
    }

    @ApiOperation("删除课程")
    @PostMapping("/del")
    public RVO<CourseVo> delCourse(@RequestBody CourseDto courseDto){
        Course course = courseService.delCourseById(courseDto.getId());
        return RVO.success(CourseVo.create(course));
    }

    @ApiOperation("修改课程")
    @PostMapping("/update")
    public RVO<CourseVo> updateCourse(@RequestBody @Valid CourseDto courseDto){
        Course course = courseService.updateCourse(courseDto);
        return RVO.success(CourseVo.create(course));
    }

    @ApiOperation("获取课程")
    @PostMapping("/get")
    public RVO<CourseVo> getCourse(@RequestBody CourseDto courseDto){
        Course course = courseService.getCourseById(courseDto.getId());
        if(course !=null){
           return RVO.success(CourseVo.create(course));
        }
        return RVO.success(null);
    }

    @ApiOperation("课程列表list")
    @PostMapping("/getList")
    public RVO<List<CourseVo>> getCourseList(@RequestBody CourseDto courseDto){
        List<Course> list = courseService.getCourseList(courseDto);
        if (list != null && list.size() > 0) {
            List<CourseVo> collect = list.stream().map(CourseVo::create).collect(Collectors.toList());
            return RVO.success(collect);
        }
       return RVO.success(null);
    }

    @ApiOperation("课程列表page")
    @PostMapping("/getPage")
    public  RVO<List<CourseVo>> getCoursePage(@RequestBody CourseDto courseDto){
        MyPage<Course> page = courseService.getCoursePage(courseDto);
        if (page != null && page.getContent().size()>0) {
            List<CourseVo> collect = page.getContent().stream().map(CourseVo::create).collect(Collectors.toList());
            return RVO.success(collect,page);
        }
       return  RVO.success(null);
    }

    @ApiOperation("模糊查询查询指定类型课程")
    @PostMapping("/retrieve/type")
    public RVO retrieveCourseByType(@RequestBody CourseDto courseDto){
        List<Course> list = courseService.retrieveCourseByType(courseDto);
        if (list.size() == 0){
            return RVO.success(null);
        }
        return RVO.success(list);
    }

    @ApiOperation("观看触发")
    @GetMapping("/view/{courseId}")
    public void viewCourse(@PathVariable Integer courseId){
        courseService.viewCourse(courseId);
    }

    @ApiOperation("查询当前老师下的作文课")
    @GetMapping("/retrieve/course/teacher")
    public RVO retrieveByUserId(){
        return courseService.retrieveByUserId();
    }

    @ApiOperation("查询当前课程下已完成的章节")
    @GetMapping("/retrieve/catalog/{courseId}")
    public RVO retrieveByCourseId(@PathVariable Integer courseId){
        return RVO.success(courseService.retrieveByCourseId(courseId));
    }

    @ApiOperation("统计当前作文课下的学习数据")
    @GetMapping("/retrieve/course/{courseId}")
    public RVO retrieveStudyDataByCourseId(@PathVariable Integer courseId){
        return RVO.success(courseService.retrieveStudyDataByCourseId(courseId));
    }


}